import request from '../config/request'

/**
 * 分页查询订单列表
 * @param {Object} params - 查询参数
 * @param {number} params.current - 当前页码
 * @param {number} params.size - 每页记录数
 * @param {string} [params.orderNo] - 订单编号
 * @param {number} [params.userId] - 用户ID
 * @param {number} [params.status] - 订单状态: 0-待付款, 1-待发货, 2-待收货, 3-已完成, 4-已取消, 5-待评价, 6-已评价
 * @returns {Promise} 返回订单分页数据
 */
export const getOrderPage = (params) => {
  return request.get('/api/order/page', params)
}

/**
 * 获取订单详情
 * @param {number} id - 订单ID
 * @returns {Promise} 返回订单详情数据
 */
export const getOrderDetail = (id) => {
  return request.get(`/api/order/${id}`)
}

/**
 * 更新订单收货信息
 * @param {number} id - 订单ID
 * @param {Object} data - 收货信息
 * @param {string} [data.receiverName] - 收货人姓名
 * @param {string} [data.receiverPhone] - 收货人电话
 * @param {string} [data.receiverAddress] - 收货地址
 * @returns {Promise} 返回更新结果
 */
export const updateOrderReceiver = (id, data) => {
  return request.put(`/api/order/${id}/receiver`, data)
}

/**
 * 更新订单物流状态
 * @param {number} id - 订单ID
 * @param {number} logisticsStatus - 物流状态: 0-未发货, 1-已发货, 2-配送中, 3-已签收
 * @returns {Promise} 返回更新结果
 */
export const updateLogisticsStatus = (id, logisticsStatus) => {
  return request.put(`/api/order/${id}/logistics`, null, {
    params: { logisticsStatus }
  })
}

/**
 * 根据用户ID查询订单
 * @param {number} userId - 用户ID
 * @param {number} [status] - 订单状态: 0-待付款, 1-待发货, 2-待收货, 3-已完成, 4-已取消, 5-待评价, 6-已评价
 * @returns {Promise} 返回用户订单列表
 */
export const getOrdersByUserId = (userId, status) => {
  return request.get(`/api/order/user/${userId}`, { status })
}

// 订单状态常量
export const ORDER_STATUS = {
  PENDING_PAYMENT: 0,  // 待付款
  PENDING_DELIVERY: 1, // 待发货
  PENDING_RECEIVE: 2,  // 待收货
  COMPLETED: 3,        // 已完成
  CANCELLED: 4,        // 已取消
  PENDING_REVIEW: 5,   // 待评价
  REVIEWED: 6          // 已评价
}

// 订单状态选项
export const ORDER_STATUS_OPTIONS = [
  { label: '待付款', value: ORDER_STATUS.PENDING_PAYMENT },
  { label: '待发货', value: ORDER_STATUS.PENDING_DELIVERY },
  { label: '待收货', value: ORDER_STATUS.PENDING_RECEIVE },
  { label: '已完成', value: ORDER_STATUS.COMPLETED },
  { label: '已取消', value: ORDER_STATUS.CANCELLED },
  { label: '待评价', value: ORDER_STATUS.PENDING_REVIEW },
  { label: '已评价', value: ORDER_STATUS.REVIEWED }
]

// 物流状态常量
export const LOGISTICS_STATUS = {
  PENDING: 0,    // 未发货
  SHIPPED: 1,    // 已发货
  DELIVERING: 2, // 配送中
  RECEIVED: 3    // 已签收
}

// 物流状态选项
export const LOGISTICS_STATUS_OPTIONS = [
  { label: '未发货', value: LOGISTICS_STATUS.PENDING },
  { label: '已发货', value: LOGISTICS_STATUS.SHIPPED },
  { label: '配送中', value: LOGISTICS_STATUS.DELIVERING },
  { label: '已签收', value: LOGISTICS_STATUS.RECEIVED }
]

